package com.booktrade.controller;

import com.booktrade.entity.Notifications;
import com.booktrade.result.Result;
import com.booktrade.service.INotificationsService;
import com.booktrade.util.BaseContext;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 消息通知表，存储系统发送给用户的通知信息 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/notifications")
@RequiredArgsConstructor
public class NotificationsController {
    private final INotificationsService notificationService;

    @GetMapping("/list/not")
    @Operation(summary = "获取用户的未读通知")
    public Result<List<Notifications>> getNoReadmessage() {
        Integer userId = BaseContext.getCurrentId();
        List<Notifications> notifications = notificationService.getNoReadmessage(userId);
        return Result.success(notifications);
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户的所有通知")
    public Result<List<Notifications>> getNotifications() {
        Integer userId = BaseContext.getCurrentId();
        List<Notifications> notifications = notificationService.getNotificationsByUserId(userId);
        return Result.success(notifications);
    }


    @PostMapping("/markAsRead")
    @Operation(summary = "标记消息为已读")
    public Result markAsRead(@RequestParam Integer notificationId) {
        notificationService.markAsRead(notificationId);
        return Result.success();
    }

}
